package pers.wz.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import pers.wz.feign.ResumeFeignClient;

@RestController
@RequestMapping("/autodeliver")
@EnableCircuitBreaker
public class AutoDeliverController {

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private ResumeFeignClient resumeFeignClient;

    @GetMapping("/checkState/{userId}")
    public Integer findResumeOpenState(@PathVariable Long userId) {
        Integer forObject = restTemplate.getForObject("http://springcloud-service-resume/resume/openstate/" + userId, Integer.class);
        System.out.println("======>>>调用简历微服务，获取用户" +
                userId + "的默认简历当前状态为： " + forObject);
        return forObject;
    }

    @GetMapping("/testjwt")
    @ResponseBody
    public Object testJwt() {
        Object details = SecurityContextHolder.getContext().getAuthentication().getDetails();
        return details;
    }

    /**
     * 使用Hystrix，通过配置超时时间，方法超过2秒未响应，就直接抛异常。
     * 原方法还是会返回，只是下游的方法依然会执行返回。
     *
     * @param userId
     * @return
     */
    @HystrixCommand(
            commandProperties = {
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000")
            }
    )
    @GetMapping("/checkState01/{userId}")
    public Integer findResumeOpenStateHystrix(@PathVariable Long userId) {
        Integer forObject = restTemplate.getForObject("http://springcloud-service-resume/resume/openstate/" + userId, Integer.class);
        System.out.println("======>>>调用简历微服务，获取用户" +
                userId + "的默认简历当前状态为： " + forObject);
        return forObject;
    }

    /**
     * 使用Hystrix，通过配置超时时间，方法超过2秒未响应，返回兜底数据。
     * 原方法还是会返回，只是下游的方法依然会执行返回。
     *
     * @param userId
     * @return
     */
    @HystrixCommand(
            commandProperties = {
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000")
            },
            fallbackMethod = "fallbackMethod"
    )
    @GetMapping("/checkState02/{userId}")
    public Integer findResumeOpenStateHystrix02(@PathVariable Long userId) {
        Integer forObject = restTemplate.getForObject("http://springcloud-service-resume/resume/openstate/" + userId, Integer.class);
        System.out.println("======>>>调用简历微服务，获取用户" +
                userId + "的默认简历当前状态为： " + forObject);
        return forObject;
    }


    /**
     * 使用Hystrix，通过配置超时时间，方法超过2秒未响应，返回兜底数据。
     * 原方法还是会返回，只是下游的方法依然会执行返回。
     *
     * @param userId
     * @return
     */
    @HystrixCommand(
            commandProperties = {
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000")
            },
            fallbackMethod = "fallbackMethod"
    )
    @GetMapping("/checkState03/{userId}")
    public Integer findResumeOpenStateHystrix03(@PathVariable Long userId) {
        Integer resume = resumeFeignClient.findResume(userId);
        System.out.println("======>>>调用简历微服务，获取用户" +
                userId + "的默认简历当前状态为： " + resume);
        return resume;
    }


    /**
     * 定义兜底方法，形参和返回值和原始方法要一致
     */
    public Integer fallbackMethod(Long userId) {
        return -1;
    }
}
